package com.linkwechat.web.service.impl;/**
 * @Author 王君平
 * @Date 2024/6/11 12:28
 * @Description
 */

import com.linkwechat.common.core.domain.model.LoginUser;
import com.linkwechat.common.utils.FeignUtil;
import com.linkwechat.common.utils.ParamCheckUtil;
import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.common.vo.CommonRsp;
import com.linkwechat.domain.WeCorpAccount;
import com.linkwechat.domain.wecom.vo.ConfigSignatureVO;
import com.linkwechat.fegin.WxApiFeignClient;
import com.linkwechat.service.IWeCorpAccountService;
import com.linkwechat.web.service.SysQwService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 王君平
 * @Date 2024/6/11 12:28
 * @Description
 */
@Service
public class SysQwServiceImpl implements SysQwService {

    @Autowired
    private WxApiFeignClient wxApiFeignClient;

    @Autowired
    private IWeCorpAccountService iWeCorpAccountService;

    @Override
    public ConfigSignatureVO getAgentConfigSignature(String url) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String corpId = loginUser.getCorpId();
        WeCorpAccount weCorpAccount = iWeCorpAccountService.getCorpAccountByCorpId(corpId);
        ParamCheckUtil.objectNull(weCorpAccount,"WeCorpAccount不能为空");
        String corpSecret = weCorpAccount.getAgentSecret();//todo wjp
        CommonRsp<String> accessTokenRes = wxApiFeignClient.accessToken(weCorpAccount.getCorpId(),corpSecret);
        String accessToken = FeignUtil.extractResult(accessTokenRes);
        ParamCheckUtil.stringEmpty(accessToken,"accessTokenRes不能为空");

        CommonRsp<String> jsapiTicketRes = wxApiFeignClient.getAgentJsapiTicket(accessToken,corpId);
        String jsapiTicket = FeignUtil.extractResult(jsapiTicketRes);
        ParamCheckUtil.stringEmpty(jsapiTicket,"jsapiTicket不能为空");
        ParamCheckUtil.stringEmpty(url,"url不能为空");
        CommonRsp<ConfigSignatureVO> res = wxApiFeignClient.getAgentConfigSignature(jsapiTicket,url);
        return FeignUtil.extractResult(res);
    }



    @Override
    public ConfigSignatureVO getWxConfigSignature(String url) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String corpId = loginUser.getCorpId();
        WeCorpAccount weCorpAccount = iWeCorpAccountService.getCorpAccountByCorpId(corpId);
        ParamCheckUtil.objectNull(weCorpAccount,"WeCorpAccount不能为空");
        String corpSecret = weCorpAccount.getAgentSecret();//todo wjp
        CommonRsp<String> accessTokenRes = wxApiFeignClient.accessToken(weCorpAccount.getCorpId(),corpSecret);
        String accessToken = FeignUtil.extractResult(accessTokenRes);
        ParamCheckUtil.stringEmpty(accessToken,"accessTokenRes不能为空");

        CommonRsp<String> jsapiTicketRes = wxApiFeignClient.getJsapiTicket(accessToken);
        String jsapiTicket = FeignUtil.extractResult(jsapiTicketRes);
        ParamCheckUtil.stringEmpty(jsapiTicket,"jsapiTicket不能为空");
        ParamCheckUtil.stringEmpty(url,"url不能为空");
        CommonRsp<ConfigSignatureVO> res = wxApiFeignClient.getConfigSignature(jsapiTicket,url);
        return FeignUtil.extractResult(res);
    }
}
